﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class MenuUser : YJ.Data.Interface.IMenuUser
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public MenuUser()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.MenuUser实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.MenuUser model)
        {
            string sql = @"INSERT INTO MenuUser
				(ID,MenuID,SubPageID,Organizes,Users,Buttons) 
				VALUES(:ID,:MenuID,:SubPageID,:Organizes,:Users,:Buttons)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID },
				new OracleParameter(":MenuID", OracleDbType.Varchar2){ Value = model.MenuID },
				new OracleParameter(":SubPageID", OracleDbType.Varchar2){ Value = model.SubPageID },
				new OracleParameter(":Organizes", OracleDbType.Varchar2){ Value = model.Organizes },
				new OracleParameter(":Users", OracleDbType.Varchar2){ Value = model.Users },
				model.Buttons == null ? new OracleParameter(":Buttons", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":Buttons", OracleDbType.Varchar2) { Value = model.Buttons }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.MenuUser实体类</param>
        public int Update(YJ.Data.Model.MenuUser model)
        {
            string sql = @"UPDATE MenuUser SET 
				MenuID=:MenuID,SubPageID=:SubPageID,Organizes=:Organizes,Users=:Users,Buttons=:Buttons
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":MenuID", OracleDbType.Varchar2){ Value = model.MenuID },
				new OracleParameter(":SubPageID", OracleDbType.Varchar2){ Value = model.SubPageID },
				new OracleParameter(":Organizes", OracleDbType.Varchar2){ Value = model.Organizes },
				new OracleParameter(":Users", OracleDbType.Varchar2){ Value = model.Users },
				model.Buttons == null ? new OracleParameter(":Buttons", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":Buttons", OracleDbType.Varchar2) { Value = model.Buttons },
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM MenuUser WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.MenuUser> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.MenuUser> List = new List<YJ.Data.Model.MenuUser>();
            YJ.Data.Model.MenuUser model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.MenuUser();
                model.ID = dataReader.GetString(0).ToGuid();
                model.MenuID = dataReader.GetString(1).ToGuid();
                model.SubPageID = dataReader.GetString(2).ToGuid();
                model.Organizes = dataReader.GetString(3);
                model.Users = dataReader.GetString(4);
                if (!dataReader.IsDBNull(5))
                    model.Buttons = dataReader.GetString(5);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.MenuUser> GetAll()
        {
            string sql = "SELECT * FROM MenuUser";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.MenuUser> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM MenuUser";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.MenuUser Get(Guid id)
        {
            string sql = "SELECT * FROM MenuUser WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.MenuUser> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByOrganizes(string organizes)
        {
            string sql = "DELETE FROM MenuUser WHERE Organizes=:Organizes";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":Organizes", OracleDbType.Varchar2){ Value = organizes }
			};
            return dbHelper.Execute(sql, parameters);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByMenuID(Guid menuID)
        {
            string sql = "DELETE FROM MenuUser WHERE MenuID=:MenuID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":MenuID", OracleDbType.Varchar2){ Value = menuID.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
    }
}